import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/16 11:09
 * @description DFS
 */
public class Solution {
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(-1), t2 = new TreeNode(-2), t3 = new TreeNode(1, t1, null), t4 = new TreeNode(3),
                t5 = new TreeNode(-2, t3, t4), t6 = new TreeNode(-3, t2, null),
                root = new TreeNode(1, t5, t6);
        Solution solution = new Solution();
        solution.pathSum(root, -4);
    }
    List<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return ans;
        }
        List<Integer> temp = new ArrayList<>();
        dfs(root, targetSum, 0, temp);
        return ans;
    }

    private void dfs(TreeNode root,  int targetSum, int sum, List<Integer> temp) {
        if (root == null) {
            return;
        }
        sum += root.val;
        temp.add(root.val);
        if (sum == targetSum && root.left == null && root.right == null) {
            ans.add(new ArrayList<>(temp));
        }
        dfs(root.left, targetSum, sum, temp);
        dfs(root.right, targetSum, sum, temp);
        temp.remove(temp.size() - 1);

    }

}
